package work.sajor.wechatpush.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import work.sajor.wechatpush.util.HttpUtil;

/**
 * @ClassName Tianqi
 *@Description TODO
 *@Author ysg
 *@Date 2024/6/2 16:00
 */
@Service
public class Tianqi {

    @Value("${weather.ak}")
    private String ak;

    @Value("${weather.district_id}")
    private String district_id;

    public JSONObject getNanjiTianqi() {
        String result = null;
        JSONObject today = new JSONObject();
        try {
            result = HttpUtil.getUrl("https://api.map.baidu.com/weather/v1/?district_id=" + district_id + "&data_type=all&ak=" + ak);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("message").equals("success")) {
                JSONArray arr = jsonObject.getJSONObject("result").getJSONArray("forecasts");
                today = arr.getJSONObject(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return today;
    }


    /**
     * @return https://api.vvhan.com/
     * 免费的天气接口
     * https://api.vvhan.com/api/weather?city=北京
     */
    public JSONObject getvvhanTianqi(String querycity) {
        String result = null;
        JSONObject todaylist = new JSONObject();
        try {
            result = HttpUtil.getUrl("https://api.vvhan.com/api/weather?city=" + querycity);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String city =  jsonObject.getString("city");
            String tip = jsonObject.getString("tip");
            JSONObject arr = jsonObject.getJSONObject("data");
            todaylist.put("city", city);
            todaylist.put("tip", tip);
            todaylist.put("date", arr.getString("date"));
            todaylist.put("type", arr.getString("type"));
            todaylist.put("week", arr.getString("week"));
            todaylist.put("high", arr.getString("high"));
            todaylist.put("low", arr.getString("low"));
            System.out.println("-----------"+todaylist);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return todaylist;
    }



    /**
     * @return https://api.vvhan.com/
     * 免费情话
     */
    public  JSONObject getCaiHongPi() {
        String result = null;
        JSONObject todaylist = new JSONObject();
        try {
            result = HttpUtil.getUrl("https://api.vvhan.com/api/text/love?type=json");
            JSONObject jsonObject = JSONObject.parseObject(result);
            JSONObject arr = jsonObject.getJSONObject("data");
            todaylist.put("linaiyuju", arr.getString("content"));
            System.out.println("-----------"+todaylist);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return todaylist;
    }

}
